package demo1;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:ArrayList的使用
 * User: LianBao
 * Date: 2024-03-22
 * Time: 9:30
 */
public class Test {

    /**
     * ArrayList 的遍历
     */
    public static void main(String[] args) {
        ArrayList<Integer> arrayList1 = new ArrayList<>();
        arrayList1.add(1);
        arrayList1.add(2);
        arrayList1.add(3);
        arrayList1.add(4);

        //1.直接sout
        System.out.println("sout直接遍历");
        System.out.println(arrayList1);
        //2.for循环
        System.out.println("for循环遍历");
        for (int i = 0; i < arrayList1.size(); i++) {
            System.out.print(arrayList1.get(i) + " ");
        }
        System.out.println();
        //3.for each
        System.out.println("for each遍历");
        for (Integer x : arrayList1) {
            System.out.print(x + " ");
        }
        System.out.println();
        //4.迭代器(顺序)
        System.out.println("iterator:迭代器遍历");
        Iterator<Integer> it = arrayList1.iterator();
        while (it.hasNext()) {
            //如果有下一个,就打印下一个
            System.out.print(it.next() + " ");
        }
        System.out.println();
        System.out.println("ListIterator:迭代器遍历");
        ListIterator<Integer> it2 = arrayList1.listIterator();
        while (it2.hasNext()) {
            //如果有下一个,就打印下一个
            System.out.print(it2.next() + " ");
        }

        System.out.println();
        System.out.println("iterator:迭代器遍历(倒序)");
        ListIterator<Integer> it3 = arrayList1.listIterator(arrayList1.size());//从后往前
        while (it3.hasPrevious()) {
            System.out.print(it3.previous() + " ");
        }
        System.out.println("掌握一种就行");
    }


    public static void main5(String[] args) {
        ArrayList<Integer> arrayList1 = new ArrayList<>();
        arrayList1.add(10);
        arrayList1.add(20);
        arrayList1.add(30);
        arrayList1.add(40);
        arrayList1.add(50);
        arrayList1.add(60);
        List<Integer> list = arrayList1.subList(1, 3);
        System.out.println(arrayList1);
        System.out.println("===================");
        list.set(0, 99);
        System.out.println("arrayList1" + arrayList1);
        System.out.println("list" + list);
    }

    public static void main4(String[] args) {
        ArrayList<Integer> arrayList1 = new ArrayList<>();
        arrayList1.add(1);
        arrayList1.add(2);
        arrayList1.add(3);
        arrayList1.add(4);
        System.out.println(arrayList1);
        arrayList1.remove(1);//删除的是1下标
        System.out.println(arrayList1);
        arrayList1.remove(Integer.valueOf(3));//删除里面的元素3
        System.out.println(arrayList1);
    }

    public static void main2(String[] args) {
        ArrayList<Integer> arrayList1 = new ArrayList<>();
        arrayList1.add(10);
        arrayList1.add(20);
        ArrayList<Integer> arrayList2 = new ArrayList<>();
        arrayList2.addAll(arrayList1);
        System.out.println(arrayList2);
    }

    public static void main1(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
//        List<Integer> list = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(1);
    }
}
